function createPostSubmit() {
	var title = document.forms["createPost"]["Title"].value;
	var imageUrl = document.forms["createPost"]["ImageUrl"].value;
	var description = document.forms["createPost"]["Description"].value;
	var zip = document.forms["createPost"]["Zip"].value;
	var categoryTitle = document.forms["createPost"]["CategoryTitle"].value;
	var sessionidPost = sessionStorage.getItem("sessionid");
	
    $.post("../svhost/Service1.svc/MakePost",
    		  {Title: title, 
    		   Link: "",
    		   ImageUrl: imageUrl, 
    		   Description: description,
    		   Zip: zip,
    		   SessionID: sessionidPost,
    		   CategoryTitle: categoryTitle},
        function(data) {
    			   var result=data.getElementsByTagName("Success")[0].childNodes[0].nodeValue;
    			   if (result === "True") {
    				   alert("New Post Created!");
    				   window.location = "boardToolbar.html"; 
    			   }
    			   else {
    				   var reason=data.getElementsByTagName("Reason")[0].childNodes[0].nodeValue;
    				   alert("Unable to Create Post: " + reason);
    			   }
    			   
    		   }
    );

}
	
function createAccountSubmit() {	
    $.post("../svhost/Service1.svc/CreateAccount",
    		$("#userRegistration").serialize(),
        function(data) {
    				var result=data.getElementsByTagName("Success")[0].childNodes[0].nodeValue;
    				if (result === "True") {
    					alert("New User Account Created!");
    					window.location = "boardToolbar.html"; 
    				}
     			   	else {
    				   var reason=data.getElementsByTagName("Reason")[0].childNodes[0].nodeValue;
    				   alert("Unable to Create Account: " + reason);
     			   	}
    				
    		   }
    );

}

function createComments(postId) {
	var sessionid_out = sessionStorage.getItem("sessionid");
	
	if (typeof sessionid_out == 'undefined' || sessionid_out == null || sessionid_out == "") {
		alert("Please Log in");
	}
	else {
		var comment = document.getElementById("commentTextBox").value;
		
	    var success = $.post("../svhost/Service1.svc/MakeComment",
	    		  {PostId: postId,
	    		   SessionId: sessionid_out,
	    		   Comment: comment},
	        function(data) {
	    				var result=data.getElementsByTagName("Success")[0].childNodes[0].nodeValue;
	    				if (result === "True") {
	    					alert("Comment Sent");	
	    					$( "#dialog" ).dialog( "close" );
	    					return true;
	    				}
	     			   	else {
	    				   var reason=data.getElementsByTagName("Reason")[0].childNodes[0].nodeValue;
	    				   alert("Unable to Send Comment: " + reason);
	    				   return false;
	     			   	}
	    				
	    		   }
	    );
	}
	
	return success;
}

function getComments(postId) {
	var comments = "User Comments:<br />";
	
    $.post("../svhost/Service1.svc/GetComments",
    		  {PostId: postId},
        function(data) {
    					var com=data.getElementsByTagName("Text");
    					var userId=data.getElementsByTagName("UserId");
    					
    					if (typeof com != 'undefined') {
	    					for (var i=0; i < com.length; i++) {
	    						var c = com[i].childNodes[0].nodeValue;
	    						var u = userId[i].childNodes[0].nodeValue;
	    						comments += u + ': ' + c + '<br />';
	    					}
	    					addComments(comments);
    					}
    		  }

    );
	
}

function logmein()
{	
	//$("#loginForm").serialize(),
	var userid2 = document.forms["loginForm"]["userid"].value;
	var password2 = document.forms["loginForm"]["password"].value;
	
	$.post("../svhost/Service1.svc/Login",	
  		  {userid: userid2, 
		   password: password2},
        function(data) {
    				var result=data.getElementsByTagName("Success")[0].childNodes[0].nodeValue;
    				if (result === "True") {
    					alert("Successful Authentication!");
    					sessionStorage.setItem("user", userid2);
    					var sessionid = data.getElementsByTagName("SessionIdentifier")[0].childNodes[0].nodeValue;
    					sessionStorage.setItem("sessionid", sessionid);
    					window.location = "boardToolbar.html"; 
    				}
     			   	else {
    				   var reason=data.getElementsByTagName("Reason")[0].childNodes[0].nodeValue;
    				   alert("Unable to Authenticate: " + reason);
     			   	}
    				
    		   }
    );
	
	
	//window.location.href = "boardToolbar.html";
}

function logmeout()
{	
	var sessionid_out = sessionStorage.getItem("sessionid");
	sessionStorage.setItem("sessionid", "");
	sessionStorage.setItem("user", "");
	
	var success = $.post("../svhost/Service1.svc/Logout",	
  		  {SessionId: sessionid_out},
        function(data) {
  			window.location = "boardToolbar.html";
  		  }
  		);
}

function rateUp(postId)
{	
	var sessionid_rate = sessionStorage.getItem("sessionid");
	var rating = "+1";
	
	var success = $.post("../svhost/Service1.svc/RatePost",	
  		  {SessionId: sessionid_rate,
			Rating: rating,
			PostId: postId},
        function(data) {
				var result=data.getElementsByTagName("Success")[0].childNodes[0].nodeValue;
				if (result === "True") {
					alert("Successful Rate!");
				}
				else {
					alert("Unsuccessful Rate!");
				}
  		  }
  		);
}

function rateDown(postId)
{	
	var sessionid_rate = sessionStorage.getItem("sessionid");
	var rating = "-1";
	
	var success = $.post("../svhost/Service1.svc/RatePost",	
  		  {SessionId: sessionid_rate,
			Rating: rating,
			PostId: postId},
        function(data) {
				var result=data.getElementsByTagName("Success")[0].childNodes[0].nodeValue;
				if (result === "True") {
					alert("Successful Rate!");
				}
				else {
					alert("Unsuccessful Rate!");
				}
  		  }
  		);
}